package com.ml.mall.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ml.mall.bean.page.Page;
import com.ml.mall.bean.req.*;
import com.ml.mall.bean.req.admin.AdminIndustryQueryOneReq;
import com.ml.mall.bean.req.admin.AdminIndustryQueryPageReq;
import com.ml.mall.bean.req.admin.AdminIndustrySupplierQueryPageReq;
import com.ml.mall.bean.req.admin.category.AdminCategoryQueryOneReq;
import com.ml.mall.bean.req.admin.category.AdminCategoryQueryPageReq;
import com.ml.mall.bean.req.admin.category.CategoryQuerySubcategoryTotalfeeReq;
import com.ml.mall.bean.req.admin.coupon.CouponQueryOneReq;
import com.ml.mall.bean.req.admin.supplier.SupplierQueryOneByCategoryReq;
import com.ml.mall.bean.req.admin.supplier.SupplierQueryOneByPurchaseReq;
import com.ml.mall.bean.req.admin.supplier.SupplierQueryOneReq;
import com.ml.mall.bean.req.admin.supplier.SupplierQueryPageReq;
import com.ml.mall.bean.req.admin.variant.VariantQueryOneReq;
import com.ml.mall.bean.req.admin.variant.VariantQueryPageReq;
import com.ml.mall.bean.req.admin.purchase.PurchaseQueryOneReq;
import com.ml.mall.bean.req.admin.purchase.PurchaseQueryReportPageReq;
import com.ml.mall.bean.req.admin.purchase.PurchaseQuerySpentCategoryReq;
import com.ml.mall.bean.res.admin.*;
import com.ml.mall.bean.res.admin.category.AdminCategoryQueryPageRes;
import com.ml.mall.bean.res.admin.category.AdminCategoryQueryReportPageRes;
import com.ml.mall.bean.res.admin.category.CategoryQuerySubcategoryTotalfeeRes;
import com.ml.mall.bean.res.admin.variant.VariantQueryOneRes;
import com.ml.mall.bean.res.admin.variant.VariantQueryPageRes;
import com.ml.mall.bean.res.admin.variant.VariantQueryTopRes;
import com.ml.mall.bean.res.admin.variant.vo.VariantQueryOneVO;
import com.ml.mall.bean.res.admin.variant.vo.VariantQueryTopVO;
import com.ml.mall.bean.res.admin.vo.AdminOverviewSalesByCategoryVO;
import com.ml.mall.bean.res.admin.vo.AdminOverviewSalesByCountryVO;
import com.ml.mall.bean.res.admin.vo.AdminOverviewSalesByIndustryVO;
import com.ml.mall.bean.res.admin.supplier.*;
import com.ml.mall.bean.res.admin.supplier.vo.SupplierQueryOneVO;
import com.ml.mall.bean.res.admin.vo.TypeSalesAndTotalSalesRes;
import com.ml.mall.bean.vo.CouponVO;
import com.ml.mall.bean.vo.DateValueVO;
import com.ml.mall.consts.CommonConsts;
import com.ml.mall.consts.OverviewTopConsts;
import com.ml.mall.conversion.AnalyticsAdminServiceHelp;
import com.ml.mall.entity.category.SysACategory;
import com.ml.mall.entity.variant.SysProductVariant;
import com.ml.mall.service.AnalyticsAdminService;
import com.ml.mall.service.asyn.AnalyticsAdminServiceAsyn;
import com.ml.mall.service.category.SysACategoryService;
import com.ml.mall.service.variant.SysProductVariantService;
import com.ml.mall.utils.DateTransformUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.*;
import java.util.concurrent.Future;
import java.util.stream.Collectors;

/**
 * Created by kyyang on 2021/6/8 22:29
 */
@Slf4j
@Service
public class AnalyticsAdminServiceImpl implements AnalyticsAdminService {

    @Autowired
    private AnalyticsAdminServiceAsyn analyticsAdminServiceAsyn;
    @Autowired
    private SysProductVariantService sysProductVariantService;
    @Autowired
    private SysACategoryService sysACategoryService;


    @Override
    public AdminOverviewRes getAdminOverview(BaseDateRangeReq req) throws Exception {

        //查询 时间范围内 金额
        Future<BigDecimal> amountFuture = analyticsAdminServiceAsyn.selectSalesByTimeLine(req.getTimeBegin(),req.getTimeEnd());
        // 州属销售额 top N
        Future<List<AdminOverviewSalesByCountryVO>> adminOverviewSalesBySateVOFuture = analyticsAdminServiceAsyn.selectSalesByStatesNameTopN(req.getTimeBegin(),req.getTimeEnd(), null);
        //按照分类 top N
        Future<List<AdminOverviewSalesByCategoryVO>> adminOverviewSalesByCategoryVOFuture = analyticsAdminServiceAsyn.selectSalesByCategoryTopN(OverviewTopConsts.ADMIN_OVERVIEW_STATE_TOP_N,req.getTimeBegin(),req.getTimeEnd(), null, null);
        //按照产业 top N
        Future<List<AdminOverviewSalesByIndustryVO>> adminOverviewSalesByIndustryVOFuture = analyticsAdminServiceAsyn.selectSalesByIndustryTopN(OverviewTopConsts.ADMIN_OVERVIEW_STATE_TOP_N,req.getTimeBegin(),req.getTimeEnd());
        // 时间范围内 进货公司注册量
        Future<List<DateValueVO>> registerCompanyBuyerFuture = analyticsAdminServiceAsyn.selectRegisterCompanyByDateRange(req.getTimeBegin(),req.getTimeEnd(), CommonConsts.COMPANY_TYPE_BUYER);
        // 时间范围内 供货公司注册量
        Future<List<DateValueVO>> registerCompanySupplierFuture = analyticsAdminServiceAsyn.selectRegisterCompanyByDateRange(req.getTimeBegin(),req.getTimeEnd(), CommonConsts.COMPANY_TYPE_SUPPLIER);
        //时间范围内 券使用
        Future<List<CouponVO>> couponUseFuture = analyticsAdminServiceAsyn.selectCouponUsesByDateRange(req.getTimeBegin(),req.getTimeEnd());

        return AnalyticsAdminServiceHelp.adminOverviewConversion(req,amountFuture,adminOverviewSalesBySateVOFuture,adminOverviewSalesByCategoryVOFuture,adminOverviewSalesByIndustryVOFuture,registerCompanyBuyerFuture,registerCompanySupplierFuture,couponUseFuture);
    }

    @Override
    public AdminPurchaseRes getAdminPurchase(BaseDateRangeReq req) throws Exception {
        //查询时间范围内 买家订单 订单数
        Future<List<DateValueVO>> purchaseOrderArrFuture = analyticsAdminServiceAsyn.selectPurchaseOrderByTimeLine(req.getTimeBegin(),req.getTimeEnd(), null, null);

        //查询时间范围内 买家订单花费金额
        Future<List<DateValueVO>> purchaseSpentArrFuture = analyticsAdminServiceAsyn.selectPurchaseSpentByTimeLine(req.getTimeBegin(),req.getTimeEnd(),null, null);

        return AnalyticsAdminServiceHelp.adminPurchaseOrderArrConversion(req,purchaseOrderArrFuture,purchaseSpentArrFuture);
    }

    @Override
    public Page<PurchaseReportPageRes> adminPurchaseQueryReportPage(PurchaseQueryReportPageReq req, Long supplierId) throws Exception {
        Page<PurchaseReportPageRes> res = new Page<PurchaseReportPageRes>(req.getPageIndex(),req.getPageNum());
        int count = 0;
        Future<Integer> countFuture = analyticsAdminServiceAsyn.selectAdminPurchaseQueryReportPageCount(req.getTimeBegin(),req.getTimeEnd(),req.getSortType(),req.getCompanyName(),supplierId);
        if(countFuture!=null){
            count =  countFuture.get();
        }
        res.setTotal(count);
        if(count>0){
            Future<List<PurchaseReportPageRes>> purchaseReportPageResFuture = analyticsAdminServiceAsyn.selectAdminPurchaseQueryReportPageList(req.getTimeBegin(),req.getTimeEnd(),req.getSortType(),req.getCompanyName(),req.getStart(),req.getEnd(),supplierId);
            if(purchaseReportPageResFuture!=null){
                List<PurchaseReportPageRes> purchaseReportPageRes = purchaseReportPageResFuture.get();
                res.setRecords(purchaseReportPageRes);
            }
        }
        return res;
    }

    @Override
    public PurchaseQueryOneRes adminPurchaseQueryOneDetail(PurchaseQueryOneReq req) throws Exception{

        //查询时间范围内 买家订单 订单数
        Future<List<DateValueVO>> purchaseOrderArrFuture = analyticsAdminServiceAsyn.selectPurchaseOrderByTimeLine(req.getTimeBegin(),req.getTimeEnd(),req.getCompanyId(), null);

        //查询时间范围内 买家订单花费金额
        Future<List<DateValueVO>> purchaseSpentArrFuture = analyticsAdminServiceAsyn.selectPurchaseSpentByTimeLine(req.getTimeBegin(),req.getTimeEnd(),req.getCompanyId(), null);

        return AnalyticsAdminServiceHelp.adminPurchaseQueryOneDetailConversion(req,purchaseOrderArrFuture,purchaseSpentArrFuture);
    }

    @Override
    public Page<PurchaseByNameSpentRes> adminPurchaseSpentByCategory(PurchaseQuerySpentCategoryReq req)throws Exception {
        Page<PurchaseByNameSpentRes> res = new Page<PurchaseByNameSpentRes>(req.getPageIndex(),req.getPageNum());
        int count = 0;
        Future<Integer> countFuture = analyticsAdminServiceAsyn.selectAdminPurchaseSpentByCategoryPageCount(req.getTimeBegin(),req.getTimeEnd(),req.getSortType(),req.getCompanyId());
        if(countFuture!=null){
            count =  countFuture.get();
        }
        res.setTotal(count);
        if(count>0){
            Future<List<PurchaseByNameSpentRes>> purchaseReportPageResFuture = analyticsAdminServiceAsyn.selectAdminPurchaseSpentByCategoryPageList(req.getTimeBegin(),req.getTimeEnd(),req.getSortType(),req.getCompanyId(),req.getStart(),req.getEnd());
            if(purchaseReportPageResFuture!=null){
                List<PurchaseByNameSpentRes> purchaseReportPageRes = purchaseReportPageResFuture.get();
                res.setRecords(purchaseReportPageRes);
            }
        }
        return res;
    }

    @Override
    public Page<PurchaseByNameSpentRes> adminPurchaseSpentBySupplier(PurchaseQuerySpentCategoryReq req) throws Exception{
        Page<PurchaseByNameSpentRes> res = new Page<PurchaseByNameSpentRes>(req.getPageIndex(),req.getPageNum());
        int count = 0;
        Future<Integer> countFuture = analyticsAdminServiceAsyn.selectAdminPurchaseSpentBySupplierPageCount(req.getTimeBegin(),req.getTimeEnd(),req.getSortType(),req.getCompanyId());
        if(countFuture!=null){
            count =  countFuture.get();
        }
        res.setTotal(count);
        if(count>0){
            Future<List<PurchaseByNameSpentRes>> purchaseReportPageResFuture = analyticsAdminServiceAsyn.selectAdminPurchaseSpentBySupplierPageList(req.getTimeBegin(),req.getTimeEnd(),req.getSortType(),req.getCompanyId(),req.getStart(),req.getEnd());
            if(purchaseReportPageResFuture!=null){
                List<PurchaseByNameSpentRes> purchaseReportPageRes = purchaseReportPageResFuture.get();
                res.setRecords(purchaseReportPageRes);
            }
        }
        return res;
    }

    @Override
    public Page<PurchaseByProductSpentRes> adminPurchaseSpentByProduct(PurchaseQuerySpentCategoryReq req) throws Exception{
        Page<PurchaseByProductSpentRes> res = new Page<PurchaseByProductSpentRes>(req.getPageIndex(),req.getPageNum());
        int count = 0;
        Future<Integer> countFuture = analyticsAdminServiceAsyn.selectAdminPurchaseSpentByProductPageCount(req.getTimeBegin(),req.getTimeEnd(),req.getSortType(),req.getCompanyId());
        if(countFuture!=null){
            count =  countFuture.get();
        }
        res.setTotal(count);
        if(count>0){
            Future<List<PurchaseByProductSpentRes>> purchaseReportPageResFuture = analyticsAdminServiceAsyn.selectAdminPurchaseSpentByProductPageList(req.getTimeBegin(),req.getTimeEnd(),req.getSortType(),req.getCompanyId(),req.getStart(),req.getEnd());
            if(purchaseReportPageResFuture!=null){
                List<PurchaseByProductSpentRes> purchaseReportPageRes = purchaseReportPageResFuture.get();
                res.setRecords(purchaseReportPageRes);
            }
        }
        return res;
    }

    @Override
    public List<TypeSalesRes> adminIndustryQueryTop(BaseDateRangeReq req) throws Exception {

        List<TypeSalesRes> list = new ArrayList<>();
        Future<List<AdminOverviewSalesByIndustryVO>> adminOverviewSalesByIndustryVOFuture = analyticsAdminServiceAsyn.selectSalesByIndustryTopN(OverviewTopConsts.ADMIN_OVERVIEW_STATE_TOP_N,req.getTimeBegin(),req.getTimeEnd());
        if(adminOverviewSalesByIndustryVOFuture!=null){
            List<AdminOverviewSalesByIndustryVO> adminOverviewSalesByIndustryVOS = adminOverviewSalesByIndustryVOFuture.get();
            if(adminOverviewSalesByIndustryVOS.size()>0){
                for(AdminOverviewSalesByIndustryVO adminOverviewSalesByIndustryVO:adminOverviewSalesByIndustryVOS){
                    TypeSalesRes typeSalesRes = new TypeSalesRes();
                    typeSalesRes.setName(adminOverviewSalesByIndustryVO.getIndustryName());
                    typeSalesRes.setSales(adminOverviewSalesByIndustryVO.getTotalSales());
                    List<DateValueVO> dateValueVOS = analyticsAdminServiceAsyn.selectIndustryDetailByTimeLine(adminOverviewSalesByIndustryVO.getIndustryId(),req.getTimeBegin(),req.getTimeEnd());
                    Map<String, Double> collect = dateValueVOS.stream().collect(Collectors.groupingBy(e -> e.getYmd(), Collectors.summingDouble(g -> g.getValue().doubleValue())));
                    List<DateValueVO> curDateMonthDataInit = DateTransformUtils.getCurDateMonthDataInit(req.getTimeBegin(), req.getTimeEnd(), collect);
                    typeSalesRes.setDayDetail(curDateMonthDataInit);
                    list.add(typeSalesRes);
                }
            }
        }
        return list;
    }

    @Override
    public Page<AdminIndustryQueryRes> adminIndustryQueryPage(AdminIndustryQueryPageReq req)throws Exception {
        Page<AdminIndustryQueryRes> res = new Page<AdminIndustryQueryRes>(req.getPageIndex(),req.getPageNum());
        int count = 0;
        Future<Integer> countFuture = analyticsAdminServiceAsyn.selectAdminIndustryQueryPagePageCount(req.getTimeBegin(),req.getTimeEnd(),req.getSortType(),req.getIndustryName());
        if(countFuture!=null){
            count =  countFuture.get();
        }
        res.setTotal(count);
        if(count>0){
            Future<List<AdminIndustryQueryRes>> purchaseReportPageResFuture = analyticsAdminServiceAsyn.selectAdminIndustryQueryPagePageList(req.getTimeBegin(),req.getTimeEnd(),req.getSortType(),req.getIndustryName(),req.getStart(),req.getEnd());
            if(purchaseReportPageResFuture!=null){
                List<AdminIndustryQueryRes> purchaseReportPageRes = purchaseReportPageResFuture.get();
                res.setRecords(purchaseReportPageRes);
            }
        }
        return res;
    }

    @Override
    public List<DateValueVO> adminIndustryQueryOne(AdminIndustryQueryOneReq req) throws Exception {
        Future<List<DateValueVO>> adminIndustryQueryOne = analyticsAdminServiceAsyn.selectAdminIndustryQueryOne(req.getTimeBegin(),req.getTimeEnd(),req.getIndustryId());
        if(adminIndustryQueryOne!=null){
            List<DateValueVO> dateValueVOS = adminIndustryQueryOne.get();
            Map<String, Double> collect = dateValueVOS.stream().collect(Collectors.groupingBy(e -> e.getYmd(), Collectors.summingDouble(g -> g.getValue().doubleValue())));
            List<DateValueVO> curDateMonthDataInit = DateTransformUtils.getCurDateMonthDataInit(req.getTimeBegin(), req.getTimeEnd(), collect);
            return curDateMonthDataInit;
        }
        return Collections.emptyList();
    }

    @Override
    public TypeSalesAndTotalSalesRes adminCategoryQueryTop(BaseDateRangeReq req, Long supplierId) throws Exception {
        TypeSalesAndTotalSalesRes typeSalesAndTotalSalesRes=new TypeSalesAndTotalSalesRes();
        List<TypeSalesRes> list = new ArrayList<>();
        Future<List<AdminOverviewSalesByCategoryVO>> adminOverviewSalesByCategoryVOFuture = analyticsAdminServiceAsyn.selectSalesByCategoryTopN(OverviewTopConsts.ADMIN_OVERVIEW_STATE_TOP_N,req.getTimeBegin(),req.getTimeEnd(),supplierId, null);
        if(adminOverviewSalesByCategoryVOFuture!=null){
            List<AdminOverviewSalesByCategoryVO> adminOverviewSalesByIndustryVOS = adminOverviewSalesByCategoryVOFuture.get();
            if(adminOverviewSalesByIndustryVOS.size()>0){
                BigDecimal totalSales=BigDecimal.ZERO;
                for(AdminOverviewSalesByCategoryVO adminOverviewSalesByCategoryVO:adminOverviewSalesByIndustryVOS){
                    TypeSalesRes typeSalesRes = new TypeSalesRes();
                    typeSalesRes.setName(adminOverviewSalesByCategoryVO.getCategoryName());
                    typeSalesRes.setTypeId(adminOverviewSalesByCategoryVO.getCategoryId());
                    typeSalesRes.setSales(adminOverviewSalesByCategoryVO.getTotalSales());
                    List<DateValueVO> dateValueVOS = analyticsAdminServiceAsyn.selectCategoryDetailByTimeLine(adminOverviewSalesByCategoryVO.getCategoryId(),req.getTimeBegin(),req.getTimeEnd(),supplierId);
                    Map<String, Double> collect = dateValueVOS.stream().collect(Collectors.groupingBy(e -> e.getYmd(), Collectors.summingDouble(g -> g.getValue().doubleValue())));
                    List<DateValueVO> curDateMonthDataInit = DateTransformUtils.getCurDateMonthDataInit(req.getTimeBegin(), req.getTimeEnd(), collect);
                    typeSalesRes.setDayDetail(curDateMonthDataInit);
                    totalSales=totalSales.add(typeSalesRes.getSales());
                    list.add(typeSalesRes);
                }
                typeSalesAndTotalSalesRes.setTotalSales(totalSales);
                typeSalesAndTotalSalesRes.setTypeSalesRes(list);
            }
        }
        return typeSalesAndTotalSalesRes;
    }

    @Override
    public Page<AdminCategoryQueryPageRes> adminCategoryQueryPage(AdminCategoryQueryPageReq req, Long supplierId) throws Exception {
        Page<AdminCategoryQueryPageRes> res = new Page<AdminCategoryQueryPageRes>(req.getPageIndex(), req.getPageNum());
        Future<List<AdminCategoryQueryPageRes>> purchaseReportPageResFuture = analyticsAdminServiceAsyn.selectAdminCategoryQueryPageList(req.getCategoryId(), req.getTimeBegin(), req.getTimeEnd(), req.getSortType(), req.getCategoryName(), req.getStart(), req.getEnd(), supplierId);
        if (purchaseReportPageResFuture != null) {
            List<AdminCategoryQueryPageRes> purchaseReportPageRes = purchaseReportPageResFuture.get();
            List<AdminCategoryQueryPageRes> list = new ArrayList<>();
            QueryWrapper<SysACategory> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("parent_id", req.getCategoryId());
            List<SysACategory> sysACategoryList = sysACategoryService.list(queryWrapper);
            HashMap<String, AdminCategoryQueryPageRes> map = new HashMap<>();
            for (AdminCategoryQueryPageRes purchaseReportPageRe : purchaseReportPageRes) {
                for (SysACategory sysACategory : sysACategoryList) {
                    if (checkCategoryId(purchaseReportPageRe.getCategoryId(),sysACategory.getCategoryId())) {
                        if (map.containsKey(sysACategory.getCategoryId())) {
                            map.get(sysACategory.getCategoryId()).setTotalSales(map.get(sysACategory.getCategoryId()).getTotalSales().add(purchaseReportPageRe.getTotalSales()));
                        } else {
                            AdminCategoryQueryPageRes adminCategoryQueryPageRes = new AdminCategoryQueryPageRes();
                            adminCategoryQueryPageRes.setCategoryId(sysACategory.getCategoryId());
                            adminCategoryQueryPageRes.setCategoryName(sysACategory.getCategoryName());
                            adminCategoryQueryPageRes.setTotalSales(purchaseReportPageRe.getTotalSales());
                            map.put(sysACategory.getCategoryId(), adminCategoryQueryPageRes);
                        }
                    }
                }
            }
            if (map.keySet().size() > 0) {
                map.keySet().forEach(it -> {
                    list.add(map.get(it));
                });
            }
            res.setRecords(list);
        }
        return res;
    }

    private boolean checkCategoryId(String categoryId, String categoryId1) {
        String[] cs = categoryId.split("C");
        String[] cs1 = categoryId1.split("C");
        boolean flag=false;
        if (cs1.length<=cs.length){
            for (int i = 0; i < cs1.length; i++) {
                flag=cs1[i].equals(cs[i]);
                if (false == flag){
                    return false;
                }
            }
        }
        return flag;
    }

    @Override
    public Page<AdminIndustrySupplierQueryRes> admiIndustrySupplierQueryPage(AdminIndustrySupplierQueryPageReq req) throws Exception {
        Page<AdminIndustrySupplierQueryRes> res = new Page<AdminIndustrySupplierQueryRes>(req.getPageIndex(),req.getPageNum());
        int count = 0;
        Future<Integer> countFuture = analyticsAdminServiceAsyn.selectAdminIndustrySupplierPageCount(req.getTimeBegin(),req.getTimeEnd(),req.getSupplierName(),req.getIndustryId());
        if(countFuture!=null){
            count =  countFuture.get();
        }
        res.setTotal(count);
        if(count>0){
            Future<List<AdminIndustrySupplierQueryRes>> purchaseReportPageResFuture = analyticsAdminServiceAsyn.selectAdminIndustrySupplierQueryPageList(req.getTimeBegin(),req.getTimeEnd(),req.getSortType(),req.getSupplierName(),req.getStart(),req.getEnd(),req.getIndustryId());
            if(purchaseReportPageResFuture!=null){
                List<AdminIndustrySupplierQueryRes> purchaseReportPageRes = purchaseReportPageResFuture.get();
                res.setRecords(purchaseReportPageRes);
            }
        }
        return res;
    }

    @Override
    public List<DateValueVO> adminCategoryQueryOne(AdminCategoryQueryOneReq req, Long supplierId) throws Exception{
        Future<List<DateValueVO>> adminIndustryQueryOne = analyticsAdminServiceAsyn.adminCategoryQueryOne(req.getTimeBegin(),req.getTimeEnd(),req.getCategoryId(),supplierId);
        if(adminIndustryQueryOne!=null){
            List<DateValueVO> dateValueVOS = adminIndustryQueryOne.get();
            Map<String, Double> collect = dateValueVOS.stream().collect(Collectors.groupingBy(e -> e.getYmd(), Collectors.summingDouble(g -> g.getValue().doubleValue())));
            List<DateValueVO> curDateMonthDataInit = DateTransformUtils.getCurDateMonthDataInit(req.getTimeBegin(), req.getTimeEnd(), collect);
            return curDateMonthDataInit;
        }


        return Collections.emptyList();
    }

    @Override
    public Page<AdminCategoryQueryReportPageRes> adminCategoryQueryReportPage(AdminCategoryQueryPageReq req, Long supplierId) throws Exception{
        Page<AdminCategoryQueryReportPageRes> res = new Page<>(req.getPageIndex(),req.getPageNum());
        int count = 0;
        Future<Integer> countFuture = analyticsAdminServiceAsyn.selectAdminCategoryQueryReportPageCount(req.getTimeBegin(),req.getTimeEnd(),req.getCategoryName(),req.getIndustryId(),supplierId);
        if(countFuture!=null){
            count =  countFuture.get();
        }
        res.setTotal(count);
        if(count>0){
            Future<List<AdminCategoryQueryReportPageRes>> purchaseReportPageResFuture = analyticsAdminServiceAsyn.selectAdminCategoryQueryReportPageList(req.getTimeBegin(),req.getTimeEnd(),req.getSortType(),req.getCategoryName(),req.getIndustryId(),req.getStart(),req.getEnd(),supplierId);
            if(purchaseReportPageResFuture!=null){
                List<AdminCategoryQueryReportPageRes> purchaseReportPageRes = purchaseReportPageResFuture.get();
                res.setRecords(purchaseReportPageRes);
            }
        }
        return res;
    }

    @Override
    public Page<AdminIndustrySupplierQueryRes> admiCategoryQueryOneDetailSalesSupplier(AdminIndustrySupplierQueryPageReq req) throws Exception {
        Page<AdminIndustrySupplierQueryRes> res = new Page<AdminIndustrySupplierQueryRes>(req.getPageIndex(),req.getPageNum());
        int count = 0;
        Future<Integer> countFuture = analyticsAdminServiceAsyn.selectAdminIndustrySupplierPageCount(req.getTimeBegin(),req.getTimeEnd(),req.getSupplierName(),req.getIndustryId());
        if(countFuture!=null){
            count =  countFuture.get();
        }
        res.setTotal(count);
        if(count>0){
            Future<List<AdminIndustrySupplierQueryRes>> purchaseReportPageResFuture = analyticsAdminServiceAsyn.selectAdminIndustrySupplierQueryPageList(req.getTimeBegin(),req.getTimeEnd(),req.getSortType(),req.getSupplierName(),req.getStart(),req.getEnd(),req.getIndustryId());
            if(purchaseReportPageResFuture!=null){
                List<AdminIndustrySupplierQueryRes> purchaseReportPageRes = purchaseReportPageResFuture.get();
                res.setRecords(purchaseReportPageRes);
            }
        }
        return res;
    }

    @Override
    public List<VariantQueryTopRes> adminVariantQueryTop(BaseDateRangeReq req, Long supplierId) throws Exception{
        Future<List<VariantQueryTopRes>> variantQueryTopFuture = analyticsAdminServiceAsyn.selectAdminVariantQueryTop(req,OverviewTopConsts.ADMIN_OVERVIEW_STATE_TOP_N,supplierId);
        if(variantQueryTopFuture!=null){
            List<VariantQueryTopRes> variantQueryTopRes = variantQueryTopFuture.get();
            for(VariantQueryTopRes res:variantQueryTopRes){
               List<VariantQueryTopVO> list = analyticsAdminServiceAsyn.selectAdminVariantQueryOneByTimeLine(req.getTimeBegin(),req.getTimeEnd(),res.getVariantId(),supplierId);
               res.setDayDetail(DateTransformUtils.getCurDateMonthDataInit(req.getTimeBegin(),req.getTimeEnd(),list));
            }
            return variantQueryTopRes;
        }
        return Collections.emptyList();
    }

    @Override
    public VariantQueryOneRes adminVariantQueryOne(VariantQueryOneReq req, Long supplierId) throws Exception {
        Future< List<VariantQueryOneVO>> variantQueryOneDetail = analyticsAdminServiceAsyn.selectAdminVariantQueryOneDetail(req,supplierId);
        VariantQueryOneRes res = new VariantQueryOneRes();
        if(variantQueryOneDetail!=null){
            List<VariantQueryOneVO> variantQueryOneVOS = variantQueryOneDetail.get();
            List<VariantQueryOneVO> list = DateTransformUtils.getCurDateMonthDataInit2(req.getTimeBegin(),req.getTimeEnd(),variantQueryOneVOS);
            BigDecimal a = BigDecimal.ZERO;
            Long b = 0L;
            for(VariantQueryOneVO vo:list){
                a = a.add(vo.getTotalSlaes());
                b+=vo.getQuantity();
                VariantQueryOneVO v = analyticsAdminServiceAsyn.selectVariantPriceAndSalesByYmd(req.getVariantId(),vo.getYmd(),supplierId);
                if(v!=null){
                    vo.setPrice(v.getPrice());
                    vo.setSalesPrice(v.getSalesPrice());
                }
            }
            //判断list;如果有空往前取；如果第一条是空；往前查；前面没有往后查；
            if(list.get(0).getPrice().equals(BigDecimal.ZERO)){
                //查询某个当前时间之前最新的价格；没有就返回0
                SysProductVariant sysProductVariant = sysProductVariantService.sysTimeBeforeMoney(list.get(0).getYmd(),Long.valueOf(req.getVariantId()));
                BigDecimal price = sysProductVariant==null?BigDecimal.ZERO:sysProductVariant.getPrice();
                list.get(0).setPrice(price);
            }
            //便利如果是空的就使用前一个；
            for(int i = 0; i < list.size();i++){
                if(list.get(i).getPrice().equals(BigDecimal.ZERO)){
                    for(int j = i; j >=0;j--){
                        if(!list.get(j).getPrice().equals(BigDecimal.ZERO)){
                            list.get(i).setPrice(list.get(j).getPrice());
                        }
                    }
                }
            }
            res.setDayDetail(list);
            res.setTotalSales(a);
            res.setTotalQuantity(b);
        }
        return res;
    }

    @Override
    public List<CategoryQuerySubcategoryTotalfeeRes> adminCategoryQuerySubcategoryTotalfee(CategoryQuerySubcategoryTotalfeeReq req) throws Exception{
        Future<List<CategoryQuerySubcategoryTotalfeeRes>> categoryQuerySubcategoryTotalfeeResFuture = analyticsAdminServiceAsyn.selectAdminCategoryQuerySubcategoryTotalfee(req.getTimeBegin(),req.getTimeEnd(),req.getCategoryId());
        if(categoryQuerySubcategoryTotalfeeResFuture!=null){
            return categoryQuerySubcategoryTotalfeeResFuture.get();
        }
        return Collections.emptyList();
    }

    @Override
    public Page<VariantQueryPageRes> adminVariantQueryPage(VariantQueryPageReq req, Long supplierId) throws Exception {
        Page<VariantQueryPageRes> res = new Page<VariantQueryPageRes>(req.getPageIndex(),req.getPageNum());
        int count = 0;
        Future<Integer> countFuture = analyticsAdminServiceAsyn.selectAdminVariantQueryPageCount(req,supplierId);
        if(countFuture!=null){
            count =  countFuture.get();
        }
        res.setTotal(count);
        if(count>0){
            Future<List<VariantQueryPageRes>> purchaseReportPageResFuture = analyticsAdminServiceAsyn.selectAdminVariantQueryPageList(req,supplierId);
            if(purchaseReportPageResFuture!=null){
                List<VariantQueryPageRes> purchaseReportPageRes = purchaseReportPageResFuture.get();
                res.setRecords(purchaseReportPageRes);
            }
        }
        return res;
    }

    @Override
    public List<SupplierQueryReportRes> adminSupplierQueryReport(BaseDateRangeReq req) throws Exception {
        Future<List<DateValueVO>> listFuture = analyticsAdminServiceAsyn.adminSupplierQueryReport(req.getTimeBegin(),req.getTimeEnd());
        List<SupplierQueryReportRes> list = new ArrayList<>();
        if(listFuture!=null){
            List<DateValueVO> dateValueVOS = listFuture.get();
            Map<String, List<DateValueVO>> collect = dateValueVOS.stream().collect(Collectors.groupingBy(DateValueVO::getName));
            for(Map.Entry<String,List<DateValueVO>> entry:collect.entrySet()){
                String key = entry.getKey();
                List<DateValueVO> value = entry.getValue();
                SupplierQueryReportRes res = new SupplierQueryReportRes();
                res.setSupplierName(key);
                Double dv = value.stream().mapToDouble(e->e.getValue().doubleValue()).sum();
                res.setTotalSales(BigDecimal.valueOf(dv));
                Map<String, Double> rs = value.stream().collect(Collectors.groupingBy(e -> e.getYmd(), Collectors.summingDouble(g -> g.getValue().doubleValue())));
                res.setDayDetail(DateTransformUtils.getCurDateMonthDataInit(req.getTimeBegin(),req.getTimeEnd(),rs));
                list.add(res);
            }
        }
        return list;
    }

    @Override
    public Page<SupplierQueryPageRes> adminSupplierQueryPage(SupplierQueryPageReq req) throws Exception {
        Page<SupplierQueryPageRes> res = new Page<SupplierQueryPageRes>(req.getPageIndex(),req.getPageNum());
        int count = 0;
        Future<Integer> countFuture = analyticsAdminServiceAsyn.adminSupplierQueryPageCount(req);
        if(countFuture!=null){
            count =  countFuture.get();
        }
        res.setTotal(count);
        if(count>0){
            Future<List<SupplierQueryPageRes>> purchaseReportPageResFuture = analyticsAdminServiceAsyn.adminSupplierQueryPageList(req);
            if(purchaseReportPageResFuture!=null){
                List<SupplierQueryPageRes> purchaseReportPageRes = purchaseReportPageResFuture.get();
                res.setRecords(purchaseReportPageRes);
            }
        }
        return res;
    }

    @Override
    public SupplierQueryOneRes adminSupplierQueryOne(SupplierQueryOneReq req) throws Exception {
        SupplierQueryOneRes res = new SupplierQueryOneRes();
        Future<List<SupplierQueryOneVO>> purchaseReportPageResFuture = analyticsAdminServiceAsyn.adminSupplierQueryOne(req);
        if(purchaseReportPageResFuture!=null){
            List<SupplierQueryOneVO> supplierQueryPageRes = purchaseReportPageResFuture.get();
            long po = supplierQueryPageRes.stream().mapToLong(e -> e.getTotalPo()).sum();
            double sales = supplierQueryPageRes.stream().mapToDouble(e -> e.getTotalSales().doubleValue()).sum();
            res.setTotalPo(po);
            res.setTotalSales(BigDecimal.valueOf(sales));
            List<SupplierQueryOneVO> list = DateTransformUtils.getCurDateMonthDataInit3(req.getTimeBegin(),req.getTimeEnd(),supplierQueryPageRes);
            res.setDayDetail(list);
        }
        return res;
    }

    @Override
    public Page<SupplierQueryOneByCategoryRes> adminSupplierQueryOneByCategory(SupplierQueryOneByCategoryReq req) throws Exception {
        Page<SupplierQueryOneByCategoryRes> res = new Page<SupplierQueryOneByCategoryRes>(req.getPageIndex(),req.getPageNum());
        int count = 0;
        Future<Integer> countFuture = analyticsAdminServiceAsyn.adminSupplierQueryOneByCategoryCount(req);
        if(countFuture!=null){
            count =  countFuture.get();
        }
        res.setTotal(count);
        if(count>0){
            Future<List<SupplierQueryOneByCategoryRes>> purchaseReportPageResFuture = analyticsAdminServiceAsyn.adminSupplierQueryOneByCategoryList(req);
            if(purchaseReportPageResFuture!=null){
                List<SupplierQueryOneByCategoryRes> purchaseReportPageRes = purchaseReportPageResFuture.get();
                res.setRecords(purchaseReportPageRes);
            }
        }
        return res;
    }

    @Override
    public Page<SupplierQueryOneByPurchaseRes> adminSupplierQueryOneByPurchasePage(SupplierQueryOneByPurchaseReq req)throws Exception {
        Page<SupplierQueryOneByPurchaseRes> res = new Page<SupplierQueryOneByPurchaseRes>(req.getPageIndex(),req.getPageNum());
        int count = 0;
        Future<Integer> countFuture = analyticsAdminServiceAsyn.adminSupplierQueryOneByPurchasePageCount(req);
        if(countFuture!=null){
            count =  countFuture.get();
        }
        res.setTotal(count);
        if(count>0){
            Future<List<SupplierQueryOneByPurchaseRes>> purchaseReportPageResFuture = analyticsAdminServiceAsyn.adminSupplierQueryOneByPurchasePageList(req);
            if(purchaseReportPageResFuture!=null){
                List<SupplierQueryOneByPurchaseRes> purchaseReportPageRes = purchaseReportPageResFuture.get();
                res.setRecords(purchaseReportPageRes);
            }
        }
        return res;
    }

    @Override
    public List<DateValueVO> adminCouponQueryOne(CouponQueryOneReq req) throws Exception {

        Future<List<DateValueVO>> adminCouponQueryOneFuture = analyticsAdminServiceAsyn.selectAdminCouponQueryOne(req);
        if(adminCouponQueryOneFuture!=null){
            List<DateValueVO> dateValueVOS = adminCouponQueryOneFuture.get();
            Map<String, Double> collect = dateValueVOS.stream().collect(Collectors.groupingBy(e -> e.getYmd(), Collectors.summingDouble(g -> g.getValue().doubleValue())));
            List<DateValueVO> curDateMonthDataInit = DateTransformUtils.getCurDateMonthDataInit(req.getTimeBegin(), req.getTimeEnd(), collect);
            return curDateMonthDataInit;
        }


        return Collections.emptyList();
    }


}
